package com.taobao.snowsnow.taobao;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import org.apache.log4j.Logger;

import com.taobao.snowsnow.hibernate.pojo.Express;
import com.taobao.snowsnow.hibernate.pojo.ExpressFieldPosition;
import com.taobao.snowsnow.hibernate.pojo.ExpressPage;
import com.taobao.snowsnow.util.Util;

/**
 * <p>打印快递单<p>
 * @author qinanhg
 * 创建日期: 2012-3-12 下午12:23:54
 */
public class PrintKd implements Printable {
	private ExpressFieldPosition fieldPosition;
	private ExpressPage expressPage;
	private static Logger logger = Logger.getLogger(PrintKd.class);
	private static float TRIM_Y = 0f; 
	
	public PrintKd(Express express, ExpressPage expressPage) {
		this.fieldPosition = express.getExpressfieldposition();
		this.expressPage = expressPage;
		
		printTask();
	}
	
	/**
	 * <p>设置打印属性<p>
	 */
	private void printTask() {
		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();

		pf.setOrientation(PageFormat.PORTRAIT);
		
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();

		
		float oneMM = (float) (72 / 25.4);
		double x = fieldPosition.getHeight() + 0.7;
		
		// 纸张大小(要转换成点)
		p.setSize(fieldPosition.getWidth() * oneMM, x * oneMM); 
		
		p.setImageableArea(0, 0, fieldPosition.getWidth() * oneMM, fieldPosition.getHeight() * oneMM); 
		pf.setPaper(p);
		
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(this, pf);
		
		// 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();
		job.setJobName("Print Express page...");
		
		// 设置打印类
		job.setPageable(book);
		
		try {
			job.print();
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}

	public int print(Graphics gp, PageFormat pf, int page)
			throws PrinterException {
		Graphics2D g2 = (Graphics2D) gp;
		
		// 设置打印颜色为黑色
		g2.setPaint(Color.black); 
		
		// 当打印页号大于需要打印的总页数时，打印工作结束
		if (page >= 1) 
			return Printable.NO_SUCH_PAGE;
		
		// 转换坐标，确定打印边界
		g2.translate(pf.getImageableX(), pf.getImageableY());
		
		
		// 创建字体
		Font font = new Font("宋体", Font.PLAIN, 12);
		Font small_font = new Font("宋体", Font.PLAIN, 10);
		
		g2.setFont(font);
		
	    // 1点为1英寸的1/72,1英寸为25.4毫米,所以A4纸大致为595x842点
		// 毫米转化为点
		float oneMM = (float) (72 / 25.4); 
		
		// 打印寄件方
		g2.drawString(expressPage.getSenderName(), Util.getFieldPostionX(fieldPosition.getSenderName()) * oneMM, Util.getFieldPostionY(fieldPosition.getSenderName()) * oneMM + TRIM_Y );
		
		if(!"".equals(fieldPosition.getOriginCity()) && fieldPosition.getOriginCity() != null)
			g2.drawString(expressPage.getOriginCity(), Util.getFieldPostionX(fieldPosition.getOriginCity()) * oneMM, Util.getFieldPostionY(fieldPosition.getOriginCity()) * oneMM + TRIM_Y);
		g2.drawString(expressPage.getSenderCompany(), Util.getFieldPostionX(fieldPosition.getSenderCompany()) * oneMM, Util.getFieldPostionY(fieldPosition.getSenderCompany()) * oneMM + TRIM_Y);
		g2.drawString(expressPage.getSenderAddress1(), Util.getFieldPostionX(fieldPosition.getSenderAddress1()) * oneMM, Util.getFieldPostionY(fieldPosition.getSenderAddress1()) * oneMM + TRIM_Y);
		g2.drawString(expressPage.getSenderAddress2(), Util.getFieldPostionX(fieldPosition.getSenderAddress2()) * oneMM, Util.getFieldPostionY(fieldPosition.getSenderAddress2()) * oneMM + TRIM_Y);
		g2.drawString(expressPage.getSenderPhone(), Util.getFieldPostionX(fieldPosition.getSenderPhone()) * oneMM, Util.getFieldPostionY(fieldPosition.getSenderPhone()) * oneMM + TRIM_Y);
		
		 
		 g2.drawString(expressPage.getSellGoodsName(), Util.getFieldPostionX(fieldPosition.getSellGoodsName()) * oneMM, Util.getFieldPostionY(fieldPosition.getSellGoodsName()) * oneMM + TRIM_Y);
		
		if(!"".equals(fieldPosition.getSenderSign()) && fieldPosition.getSenderSign() != null && expressPage.getSenderSign() != null)
			g2.drawString(expressPage.getSenderSign(), Util.getFieldPostionX(fieldPosition.getSenderSign()) * oneMM, Util.getFieldPostionY(fieldPosition.getSenderSign()) * oneMM + TRIM_Y);
		if(!"".equals(fieldPosition.getYear()) && fieldPosition.getYear() != null)
			g2.drawString(expressPage.getYear(), Util.getFieldPostionX(fieldPosition.getYear()) * oneMM, Util.getFieldPostionY(fieldPosition.getYear()) * oneMM + TRIM_Y);
		g2.drawString(expressPage.getMonth(), Util.getFieldPostionX(fieldPosition.getMonth()) * oneMM, Util.getFieldPostionY(fieldPosition.getMonth()) * oneMM + TRIM_Y);
		g2.drawString(expressPage.getDay(), Util.getFieldPostionX(fieldPosition.getDay()) * oneMM, Util.getFieldPostionY(fieldPosition.getDay()) * oneMM + TRIM_Y);
		
		// 打印收件方
		g2.drawString(expressPage.getReceiverName(), Util.getFieldPostionX(fieldPosition.getReceiverName()) * oneMM, Util.getFieldPostionY(fieldPosition.getReceiverName()) * oneMM + TRIM_Y);
		if(expressPage.getDestinationCity() != null && !"".equals(fieldPosition.getDestinationCity()) && fieldPosition.getDestinationCity() != null)
			g2.drawString(expressPage.getDestinationCity(), Util.getFieldPostionX(fieldPosition.getDestinationCity()) * oneMM, Util.getFieldPostionY(fieldPosition.getDestinationCity()) * oneMM + TRIM_Y);
		g2.drawString(expressPage.getReceiverCompany(), Util.getFieldPostionX(fieldPosition.getReceiverCompany()) * oneMM, Util.getFieldPostionY(fieldPosition.getReceiverCompany()) * oneMM + TRIM_Y);
		
		String address = expressPage.getReceiverAddress();
		
		g2.setFont(small_font);
		
		float address_font_width = g2.getFontMetrics().stringWidth(address);
		float address_field_width = Util.getFieldPostionWidth(fieldPosition.getReceiverAddress1()) * oneMM;
		
		if(address_font_width - address_field_width <= 0) {
			g2.drawString(address, Util.getFieldPostionX(fieldPosition.getReceiverAddress1()) * oneMM, Util.getFieldPostionY(fieldPosition.getReceiverAddress1()) * oneMM + TRIM_Y);
		} else {
			int a = (int) (address_font_width - address_field_width) / small_font.getSize() + 3;
			String address1 = address.substring(0, address.length() - a);
			String address2 = address.substring(address.length() - a);
			g2.drawString(address1, Util.getFieldPostionX(fieldPosition.getReceiverAddress1()) * oneMM, Util.getFieldPostionY(fieldPosition.getReceiverAddress1()) * oneMM + TRIM_Y);
			g2.drawString(address2, Util.getFieldPostionX(fieldPosition.getReceiverAddress2()) * oneMM, Util.getFieldPostionY(fieldPosition.getReceiverAddress2()) * oneMM + TRIM_Y);
		}
		
		g2.setFont(font);
		
		String phone = expressPage.getReceiverPhone();
		
		g2.drawString(phone, Util.getFieldPostionX(fieldPosition.getReceiverPhone()) * oneMM, Util.getFieldPostionY(fieldPosition.getReceiverPhone()) * oneMM + TRIM_Y);
		
		return Printable.PAGE_EXISTS; // 存在打印页时，继续打印工作
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	}
}

